package com.example.controller;


import com.example.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import java.util.concurrent.Future;

@RestController
public class MyController {
    @Autowired
    MyService myService;

    @GetMapping("/sendSMS")
    public String sendSMS() throws Exception {
        Long startTime = System.currentTimeMillis();
        myService.sendSMS();
        Long endTime = System.currentTimeMillis();
        System.out.println("主流程耗时： " + (endTime - startTime));
        return "success";
    }
//    主流程耗时： 7  主线程没有被阻塞 先运行完成
//    调用短信验证码业务方法...  子线程做自己的耗时任务
//    短信业务执行完成耗时：5002


    @GetMapping("/statistics")
    public String statistics() throws Exception {
        Long startTime = System.currentTimeMillis();
        myService.sendSMS();
        Future<Integer> futureA = myService.processA();
        Future<Integer> futureB = myService.processB();
        int total = futureA.get() + futureB.get();
        System.out.println("异步任务数据统计汇总结果： " + total);
        Long endTime = System.currentTimeMillis();
        System.out.println("主流程耗时： " + (endTime - startTime));
        return "success";
    }


    @GetMapping("/testSchedul")
    public String excuteSchdul() {
        return myService.schedulTask();
    }

    @GetMapping("/schedulTaskForfixedDelay")
    public String schedulTaskForfixedDelay() {
        return myService.schedulTaskForfixedDelay();
    }

    @GetMapping("/schedulTaskForinitialDelay")
    public String schedulTaskForinitialDelay() throws Exception {
        return myService.schedulTaskForinitialDelay();
    }

    @GetMapping("/sendMail")
    @ResponseBody
    public String sendMail() {
        return myService.sendSimpleMail("2240445712@qq.com",
                "happy new year",
                "新年快乐",
                "2378163310@qq.com", "3557411859@qq.com"
        );
    }


    @GetMapping("/sendMimeMail")
    @ResponseBody
    public String sendMimeMail() throws MessagingException {
        return myService.sendMimeMail("3557411859@qq.com",
                "happy new year",
                "<html><head></head><body><h1><a href='http://www.baidu.com'>祝大家元旦快乐</a></h1>",
                "H:\\教学资料\\2020级教学资料\\ssm资料上学期\\ppt\\AriticalMapper.xml",
                "image001",
                "C:\\Users\\Administrator\\Pictures\\1.jpg");
    }


}
